// ParallaxLayer.ts
const { ccclass, property } = cc._decorator;
@ccclass
export class ParallaxLayer extends cc.Component {
    @property(cc.Vec2)
    motionScale: cc.Vec2 = cc.v2(1, 1); // 运动比例系数

    @property(cc.Vec2)
    motionMirroring: cc.Vec2 = cc.v2(0, 0); // 无限滚动镜像参数

    private _initialPos: cc.Vec3;

    onLoad() {
        this._initialPos = this.node.position.clone();
    }

    updatePosition(cameraPos: cc.Vec3, scrollOffset: cc.Vec2) {
        // 视差位移计算
        let offsetX = (cameraPos.x - this._initialPos.x) * this.motionScale.x + scrollOffset.x;
        let offsetY = (cameraPos.y - this._initialPos.y) * this.motionScale.y + scrollOffset.y;

        // 处理无限滚动镜像
        if (this.motionMirroring.x > 0) {
            offsetX %= this.motionMirroring.x;
        }
        if (this.motionMirroring.y > 0) {
            offsetY %= this.motionMirroring.y;
        }

        this.node.setPosition(
            this._initialPos.x + offsetX,
            this._initialPos.y + offsetY
        );
    }
}